(function (angular,uuid) {
    /* 
        自定义存储服务
    
    */
    angular.module('dataService', [])
        .service('DataService', function ($window) {
            // 获取数据
            this.getData = function () {
                return $window.localStorage.getItem('todoList') ? JSON.parse($window.localStorage.getItem('todoList')) : [];
            }
            // 保存数据
            this.insertData = function (data) {
                let todolist = JSON.parse($window.localStorage.getItem('todoList'));
                todolist.push({
					id: uuid.v1(),
					title: data,
					completed: false,
					editing: false,
				});
                $window.localStorage.setItem('todoList', JSON.stringify(todolist));
            }
            // 删除数据
            this.removeData = function (id) {
                let todolist = JSON.parse($window.localStorage.getItem('todoList'));
                let index = todolist.findIndex(item => item.id === id);
                todolist.splice(index, 1);
                $window.localStorage.setItem('todoList', JSON.stringify(todolist));
            }
            // 更新数据
            this.updateData = function (todo) {
                var title = todo.title.trim();
                var id = todo.id;
                let todolist = JSON.parse($window.localStorage.getItem('todoList'));
                let index = todolist.findIndex(item => item.id === id);
                todolist[index].title = title;
                todolist[index].editing = false;
                $window.localStorage.setItem('todoList', JSON.stringify(todolist));
            }
            // 清除已完成的
            this.clearCompleted = function (todoList) {
                $window.localStorage.setItem('todoList', JSON.stringify(todoList.filter(item => !item.completed)));
            }
        });
})(angular,uuid);